# -*- coding: utf-8 -*-
#===============================================================================
# Koro Business Environment
# Copyright (C) 2008  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 11/ago/2011

@author: antstud
'''

import wx
from koro import util
from koro.ui.wxgui.panels import BasePanel,OpzioniPanelBase
from koro.ui.wxgui.widgets import controls as co
from koro.ui.wxgui.mellcfg import mDefaultBorder

#import gActionPanels


#===============================================================================
# AnagraficaPanel
#===============================================================================
class AnagraficaPanel(BasePanel):
    def __init__(self,*args,**kwargs):
        BasePanel.__init__(self,*args,**kwargs)
        #mainsizer=wx.BoxSizer()
        topsizer=wx.BoxSizer(wx.VERTICAL)
        
        orsizer=wx.BoxSizer()
        self.ID = co.LabelTextCtrl(self,label='ID',size=(70,-1),mstyle=co.mTE_READ_ONLY)
        orsizer.Add(self.ID,0,wx.ALL,border=mDefaultBorder)        
        orsizer.AddSpacer(20)        
        self.desueto=wx.CheckBox(self,label='Desueto',style=wx.ALIGN_RIGHT)
        orsizer.Add(self.desueto,0,wx.ALL,border=mDefaultBorder)        
        topsizer.Add(orsizer,0)
        
        self.ragsoc = co.LabelTextCtrl(self,label='Ragione Sociale', mstyle=co.mTE_REQUIRED|co.mTE_TITLED, maxlen=100)
        topsizer.Add(self.ragsoc,0,wx.ALL,border=mDefaultBorder)
        
        self.piva = co.LabelTextCtrl(self,label='Partita Iva', maxlen=11)
        topsizer.Add(self.piva,0,wx.ALL,border=mDefaultBorder)
        
        self.codfisc = co.LabelTextCtrl(self,label='Cod.Fiscale', maxlen=16, mstyle=co.mTE_UPPER)
        topsizer.Add(self.codfisc,0,wx.ALL,border=mDefaultBorder)
        
        self.indirizzo = co.LabelTextCtrl(self,label='Indirizzo', maxlen=100)
        topsizer.Add(self.indirizzo,0,wx.ALL,border=mDefaultBorder)

        hsizer=wx.BoxSizer()
        self.cap = co.LabelTextCtrl(self,label='CAP', maxlen=8,mstyle=co.mTE_DIGIT_ONLY, size=(48,-1))
        hsizer.Add(self.cap,0,wx.ALL,border=mDefaultBorder)

        self.localita = co.LabelTextCtrl(self,label='Città',spacing=33, maxlen=50,size=(166, -1))
        hsizer.Add(self.localita,0,wx.ALL,border=mDefaultBorder)
        
        topsizer.Add(hsizer,0,wx.ALL,border=mDefaultBorder)
        
        hsizer=wx.BoxSizer()
        self.provincia = co.LabelTextCtrl(self,label='Prov',maxlen=2,mstyle=co.mTE_UPPER, size=(30, -1))
        hsizer.Add(self.provincia,0,wx.ALL,border=mDefaultBorder)
        
        self.btn_mappa=wx.Button(self,label='Mostra Mappa',style=wx.NO_3D|wx.NO_BORDER)
        self.btn_mappa.Bind(wx.EVT_BUTTON, self._mostra_mappa)
        hsizer.Add(self.btn_mappa,0,wx.LEFT,15)
        
        topsizer.Add(hsizer,0,wx.ALL,border=mDefaultBorder)
        

#        self.indirizzo = co.mTextCtrl(id=wx.ID_ANY, maxlen=50, mstyle=co.mTE_TITLED,parent=self, size=(197, -1))
#        self.cap = co.mTextCtrl(id=wx.ID_ANY, maxlen=5,mstyle=co.mTE_DIGIT_ONLY, parent=self, size=wx.Size(48,-1))
#        topsizer.Add(co.CompositeCtrl(self.indirizzo,label='Indirizzo/CAP',ctrl_1=self.cap),0,wx.ALL,border=mDefaultBorder)
#
#
#        self.localita = co.mTextCtrl(id=wx.ID_ANY, maxlen=50, mstyle=co.mTE_TITLED, parent=self, size=(197, -1))
#        self.provincia = co.mTextCtrl(id=wx.ID_ANY, maxlen=2,mstyle=co.mTE_UPPER, parent=self, size=(48, -1))
#        topsizer.Add(co.CompositeCtrl(self.localita,label=u'Località/Prov.',ctrl_1=self.provincia),0,wx.ALL,border=mDefaultBorder)        
        self.referente = co.LabelTextCtrl(self,label='Referente',maxlen=50)
        topsizer.Add(self.referente,0,wx.ALL,border=mDefaultBorder)
        
        hsizer=wx.BoxSizer()
        self.telefono = co.LabelTextCtrl(self,label='Telefono',spacing=109, maxlen=20,mstyle=co.mTE_DIGIT_ONLY,size=(108, -1))
        hsizer.Add(self.telefono,0,wx.ALL,border=mDefaultBorder)
        self.fax = co.LabelTextCtrl(self,label='Fax',spacing=32, maxlen=20,mstyle=co.mTE_DIGIT_ONLY,size=(108, -1))
        hsizer.Add(self.fax,0,wx.ALL,border=mDefaultBorder)
        topsizer.Add(hsizer,0,wx.ALL,border=mDefaultBorder)
        
        self.cellulare = co.LabelTextCtrl(self,label='Cellulare', maxlen=20,size=(100, -1), mstyle=co.mTE_DIGIT_ONLY)
        topsizer.Add(self.cellulare,0,wx.ALL,border=mDefaultBorder)

        self.email = co.LabelTextCtrl(self,label='E-Mail', maxlen=50)
        topsizer.Add(self.email,0,wx.ALL,border=mDefaultBorder)

        self.url = co.LabelTextCtrl(self,label='URL http://', maxlen=50)
        topsizer.Add(self.url,0,wx.ALL,border=mDefaultBorder)
        
#        self.pagamento = wx.ComboBox(id=wx.NewId(),parent=self,size=wx.Size(197, 25),style=wx.CB_READONLY)
#        topsizer.Add(co.CompositeCtrl(self.pagamento,label='Tipo Pagamento'),0,wx.ALL,border=mDefaultBorder)
#        
#        self.listino = co.ComboBox(id=wx.NewId(),parent=self,size=wx.Size(197, 25),style=wx.CB_READONLY,item_info='descrizione')
#        topsizer.Add(co.CompositeCtrl(self.listino,label='Listino'),0,wx.ALL,border=mDefaultBorder)
#        
#        self.iva = co.ComboBox(id=wx.NewId(),parent=self,size=wx.Size(197, 25),style=wx.CB_READONLY,item_info='descrizione')
#        topsizer.Add(co.CompositeCtrl(self.iva,label='Iva'),0,wx.ALL,border=mDefaultBorder)

        self.note = co.LabelTextCtrl(self,label='Note', maxlen=100, style=wx.TE_MULTILINE,size=(-1,50))
        topsizer.Add(self.note,0,wx.ALL,border=mDefaultBorder)   
        
        self.set_main_sizer(topsizer)
    
    def _mostra_mappa(self,evt):
        if not util.to_bool(self.indirizzo.get_value()):
            return
        via=self.indirizzo.get_value().replace(' ','+')
        citta=self.localita.get_value().replace(' ','+')
        uri="http://maps.google.it/maps?f=q&source=s_q&hl=it&q={0},+{1}".format(via,citta)
        wx.LaunchDefaultBrowser(uri)

    def update_fields(self,obj):
        print 'AnagraficaPanel.update_fields'
        self.reset_fields()
        self.ID.set_value(obj.get_ID_fmt())
        self.ragsoc.set_value(obj.ragione_sociale)
        self.desueto.SetValue(obj.is_desueto())
        i=obj.get_indirizzo()
        if i is not None:
            self.indirizzo.set_value(i.via)
            self.cap.set_value(i.cap)
            self.localita.set_value(i.localita)
            self.provincia.set_value(i.provincia)
        self.piva.set_value(obj.piva)
        self.codfisc.set_value(obj.codfisc)
        self.referente.set_value(obj.referente)
        self.telefono.set_value(obj.telefono)
        self.fax.set_value(obj.fax)
        self.cellulare.set_value(obj.cellulare)
        self.email.set_value(obj.email)
        self.url.set_value(obj.sitoweb)
        self.note.set_value(obj.note)                        

    def update_object(self,obj):
        print 'AnagraficaPanel.update_object'
        obj.set_ID(self.ID.get_value())
        obj.ragione_sociale=self.ragsoc.get_value()
        obj.desueto=self.desueto.IsChecked()
        i=obj.get_indirizzo()
        i.via=self.indirizzo.get_value()
        i.cap=self.cap.get_value()
        i.localita=self.localita.get_value()
        i.provincia=self.provincia.get_value()
        obj.set_indirizzo(i)
#        obj.listino=self.listino.get_selected_object()# .GetClientData(self.listino.GetSelection())
#        obj.iva=self.iva.get_selected_object()# .GetClientData(self.iva.GetSelection())
        obj.set_partita_iva(self.piva.get_value())
        obj.set_codice_fiscale(self.codfisc.get_value())
        obj.referente=self.referente.get_value()
        obj.telefono=self.telefono.get_value()
        obj.fax=self.fax.get_value()
        obj.cellulare=self.cellulare.get_value()
        obj.email=self.email.get_value()
        obj.sitoweb=self.url.get_value()
        obj.note=self.note.get_value()        
              
    def reset_fields(self):
        print 'AnagraficaPanel.reset_fields'
        self.ID.clear()
        self.ragsoc.clear()
        self.desueto.SetValue(False)
        self.piva.clear()
        self.codfisc.clear()
        self.indirizzo.clear()
        self.cap.clear()
        self.localita.clear()
        self.provincia.clear()
        self.telefono.clear()
        self.cellulare.clear()
        self.fax.clear()
        self.url.clear()
        self.email.clear()
        self.note.clear()
    
    def reset_focus(self):
        self.ragsoc.set_focus()
#        self.listino.set_value("")
#        self.iva.set_value("")
    
#    def fill_combobox(self):        
#        observer=wx.GetApp().get_observer()
#        #listini
#        observer.set_listini_context()
#        objects=observer.get_objects()
#        self.listino.load(objects)
#        #codici_iva
#        observer.set_codici_iva_context()
#        objects=observer.get_objects()
#        self.iva.load(objects)

#===============================================================================
# AziendaOptionPanel
#===============================================================================
class AziendaOptionPanel(OpzioniPanelBase):
    NAME="Azienda"
    def __init__(self,*args,**kwargs):
        OpzioniPanelBase.__init__(self,*args,**kwargs)
        self._dati_azienda=self._request_dati_azienda()
        self.ragione_sociale = co.LabelTextCtrl(self,size=(355, -1),label='Ragione Sociale')
        self.sottotitolo = co.LabelTextCtrl(self,size=(355, -1),label='Sottotitolo')
        self.indirizzo = co.LabelTextCtrl(parent=self,size=(355, -1),label='Indirizzo')
        self.cap = co.LabelTextCtrl(parent=self,size=(60, -1),label='CAP')
        
        self.citta = co.LabelTextCtrl(parent=self,size=(150,-1),label='Città',spacing=-1)
        self.provincia = co.LabelTextCtrl(parent=self,size=(40,-1),label='Prov.',spacing=-1)
        self.codice_fiscale = co.LabelTextCtrl(parent=self,size=(154, -1),label='Cod.Fiscale')
        
        self.piva = co.LabelTextCtrl(parent=self,size=(150, -1),label='P.Iva',spacing=-1)
        self.telefono = co.LabelTextCtrl(parent=self,size=(156, -1),label='Tel.')

        self.fax = co.LabelTextCtrl(parent=self,size=(155, -1),label='Fax',spacing=-1)
        
        self.email = co.LabelTextCtrl(parent=self,size=(355, -1),label='E-Mail')
        self.home_page = co.LabelTextCtrl(parent=self,size=(355, -1),label='Home Page')
        self.note = co.LabelTextCtrl(parent=self,style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER,label='Note',size=(-1,-1))
        
        self.__do_layout()        
           
    def __do_layout(self):
        #mainsizer=wx.BoxSizer(wx.VERTICAL)
        fieldsizer = wx.GridBagSizer(0,0)
        
        orsizer=wx.BoxSizer()
        
        boxsizer=wx.BoxSizer(wx.VERTICAL)        
        boxsizer.AddSpacer(10)
        titolo=wx.StaticText(self,label="Dati Azienda")
        font=titolo.GetFont()
        font.SetWeight(wx.FONTWEIGHT_BOLD)
        titolo.SetFont(font)
        boxsizer.Add(titolo,0,wx.ALL,border=mDefaultBorder)
        boxsizer.AddSpacer(5)
        
        self.ID=None
        
        
        boxsizer.Add(self.ragione_sociale,0,wx.ALL,border=mDefaultBorder)
        
        
        boxsizer.Add(self.sottotitolo,0,wx.ALL,border=mDefaultBorder)
        
        
        boxsizer.Add(self.indirizzo,0,wx.ALL,border=mDefaultBorder)
        
        
        orsizer.Add(self.cap,0,wx.ALL,border=mDefaultBorder)
        
        orsizer.AddSpacer(10)
        
        
        orsizer.Add(self.citta,0,wx.ALL,border=mDefaultBorder)
        
        orsizer.AddSpacer(10)
        
        
        orsizer.Add(self.provincia,0,wx.ALL,border=mDefaultBorder)
        
        boxsizer.Add(orsizer)
        
        orsizer=wx.BoxSizer()
        
        orsizer.Add(self.codice_fiscale,0,wx.ALL,border=mDefaultBorder)
        
        orsizer.AddSpacer(10)
        
        
        orsizer.Add(self.piva,0,wx.ALL,border=mDefaultBorder)
        
        boxsizer.Add(orsizer)
        
        orsizer=wx.BoxSizer()
        
        
        orsizer.Add(self.telefono,0,wx.ALL,border=mDefaultBorder)
        
        orsizer.AddSpacer(10)
        
        
        orsizer.Add(self.fax,0,wx.ALL,border=mDefaultBorder)
        
        boxsizer.Add(orsizer)
        
        
        boxsizer.Add(self.email,0,wx.ALL,border=mDefaultBorder)
        
        
        boxsizer.Add(self.home_page,0,wx.ALL,border=mDefaultBorder)
        
        
        boxsizer.Add(self.note,0,wx.ALL,border=mDefaultBorder)
                                
        fieldsizer.Add(boxsizer,(0,0),flag=wx.ALL|wx.EXPAND ,border=0)#riga zero colonna 1                
        fieldsizer.AddGrowableRow(0)
        fieldsizer.AddGrowableCol(1)
        self.set_main_sizer(fieldsizer)
        #mainsizer.Add(fieldsizer,1,flag=wx.ALL|wx.EXPAND ,border=10)#riga zero colonna 1        
        
        #mainsizer.Layout()
        self.SetAutoLayout(True)
    
    def update_fields(self,opzioni):
        print 'Azienda.update_fields'
        azienda=self._dati_azienda
        self.ID=azienda.get_ID()
        self.ragione_sociale.set_value(azienda.ragione_sociale)
        self.sottotitolo.set_value(azienda.sottotitolo)
        self.indirizzo.set_value(azienda.get_via())
        self.cap.set_value(azienda.get_cap())
        self.citta.set_value(azienda.get_citta())
        self.provincia.set_value(azienda.get_provincia())
        self.codice_fiscale.set_value(azienda.codfisc)
        self.piva.set_value(azienda.piva)
        self.telefono.set_value(azienda.telefono)
        self.fax.set_value(azienda.fax)
        #self.cellulare.SetValue(azienda.cellulare)
        self.email.set_value(azienda.email)
        self.home_page.set_value(azienda.sitoweb)
        self.note.set_value(azienda.note)
    
    def reset_fields(self):
        print 'Azienda.reset_fields'
        #self.gestione_magazzino.SetValue(False)
    
    def update_object(self,opzioni):
        print 'Azienda.update_object'
        azienda=self._dati_azienda
        azienda.set_ID(self.ID)
        azienda.ragione_sociale=self.ragione_sociale.get_value()
        azienda.sottotitolo=self.sottotitolo.get_value()
        azienda.set_via(self.indirizzo.get_value())
        azienda.set_cap(self.cap.get_value())
        azienda.set_citta(self.citta.get_value().decode('utf-8'))
        azienda.set_provincia(self.provincia.get_value())
        azienda.codfisc=self.codice_fiscale.get_value()
        azienda.piva=self.piva.get_value()
        azienda.telefono=self.telefono.get_value()
        azienda.fax=self.fax.get_value()
        #self.cellulare.SetValue(azienda.cellulare)
        azienda.email=self.email.get_value()
        azienda.sitoweb=self.home_page.get_value()
        azienda.note=self.note.get_value()
    
    def _request_dati_azienda(self):
        observer=wx.GetApp().get_observer()
        observer.set_context('AZIENDA')
        return observer.get_dati_azienda()
    
    def before_close(self,observer):
        observer.save_data('AZIENDA',self._dati_azienda)
    
    def on_page_focus(self):
        pass


#class EmptyPanel(BasePanel):
#    def __init__(self,*args,**kwargs):
#        BasePanel.__init__(self,*args,**kwargs)
#    
#    def update_fields(self,dataObj):
#        pass
#
#    def update_object(self,obj):
#        pass
#
#    def reset_fields(self):
#        pass
#    
#    def fill_combobox(self):
#        pass